<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>观察者模式</title>
</head>
<body>
<script>
    //所依赖的代码
function ObserverList(){
    this.observerList=[];
}
ObserverList.prototype.Add=function(obj){
    return this.observerList.push(obj);
};
ObserverList.prototype.RemoveIndexAt=function(index){
    this.observerList.splice(index,1);
};
ObserverList.prototype.Count=function(){
    return this.observerList.length;
};
ObserverList.prototype.Get=function(obj){
    if(index>-1&&index<this.observerList.length){
        return this.observerList[index];
    }
};
//observer:观察者：为那些在目标状态发生改变时获得通知的对象提供更新接口
//建立目标:维护一系列的观察者，方便添加或者删除观察者

//建立目标
function Subject() {
    this.observer = new ObserverList();
}
Subject.prototype.AddObserver=function(observer) {
    this.observer.Add(observer);
};
Subject.prototype.RemoveObserver=function(observer) {
    this.observer.RemoveIndexAt(this.observer.indexOf(observer));
};
Subject.prototype.Notify=function(context) {
    var observerCount = this.observer.Count();
    for (var i = 0; i < observerCount; i++) {
        this.observer.Get(i).update(context);
    }
};
    //建立Observer
function Observer() {
    this.update = function () {
        console.log('111');
    }
}

</script>
</body>
</html>